package middleware

import (
	"admin_template/bff/admin/hertz_gen/base"
	"admin_template/bff/admin/internal/pkg/utils"
	"context"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

type EnforcerFace interface {
	Enforce(ctx *app.RequestContext, path, method string) error
}

func EnforcerHandler(enforcerFace EnforcerFace) app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		path := string(ctx.Path())
		method := string(ctx.Method())
		err := enforcerFace.Enforce(ctx, path, method)
		if err != nil {
			reply := base.BaseReply{
				Code: base.ErrCode_Fail,
				Msg:  "权限不足,禁止访问,请联系管理员",
			}
			utils.Reply(ctx, consts.StatusOK, &reply)
			ctx.Abort()
			return
		}
		ctx.Next(c)
	}
}
